﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ObjectDanhMuc.DB;
using CMISLibrary;

namespace ObjectDanhMuc
{
    public class cls_D_DVI_QLY_Controller
    {
        #region   Atributes

        //private DataSet CMIS_Header = new CMISOutputParameter();
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();
        private D_DVI_QLY info;

        #endregion


        #region   Constructor

        public cls_D_DVI_QLY_Controller()
        {
            info = new D_DVI_QLY();
        }
        public cls_D_DVI_QLY_Controller(D_DVI_QLY Info)
        {
            this.info = Info;
        }

        #endregion


        #region   Properties

        public D_DVI_QLY pInfor
        {
            get { return info; }
            set { info = value; }
        }

        #endregion


        #region   Methods

        //public IEnumerable<PA_D_KET_QUA> select_VTT_All()
        //{
        //    try
        //    {
        //        var q = from p in CMIS2.DB.VTT
        //                orderby p.ID ascending
        //                select p;
        //        return q;
        //    }
        //    catch
        //    {
        //        CMIS2.ResetDB(); return null;
        //    }
        //}

        public DataTable select_SQUAN_TRI(string strMadviqly, ref string strErr)
        {
            try
            {
                CMIS2.ResetDB();
                var q = from p in CMIS2.DB.SQUAN_TRI
                        where p.MA_DVIQLY == strMadviqly
                        select new
                        {
                            p.THANG,
                            p.NAM
                        };
                return Utility.LINQToDataTable(q);
            }
            catch (Exception ex)
            {
                strErr = ex.ToString();
                CMIS2.ResetDB(); return null;
            }
        }

        public DataSet select_D_DVI_QLY(string strMadviqly)
        {
            try
            {
                var q = from p in CMIS2.DB.D_DVI_QLY
                        where p.MA_DVIQLY.Contains(strMadviqly)
                        orderby p.MA_DVIQLY, p.MA_DVICTREN
                        select new
                        {
                            p.MA_DVIQLY,
                            p.TEN_DVIQLY,
                            p.MA_DVICTREN,
                            TEN_DVIQLYCTREN = p.TEN_DVIQLY,
                            SEARCH = p.MA_DVIQLY + " --- " + p.TEN_DVIQLY
                        };
                dt = Utility.LINQToDataTable(q);
                ds.Tables.Add(dt);

                //var q2 = from p in CMIS2.DB.D_DVI_QLY where p.MA_DVIQLY == strMadviqly

                return ds;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<D_DVI_QLY> select_D_DVI_QLY_captren(string strMadviqly)
        {
            try
            {
                CMIS2.ResetDB();

                var q1 = from p in CMIS2.DB.D_DVI_QLY.ToList()
                        join m in CMIS2.DB.D_LO.ToList() on new { p.MA_DVIQLY } equals new { m.MA_DVIQLY }
                        where p.MA_DVIQLY == strMadviqly
                        && m.MA_DVIQLY == strMadviqly
                        select new 
                        {
                            p.TEN_DVIQLY,
                            m.TEN_LO
                        };

                var q = from p in CMIS2.DB.D_DVI_QLY
                        where p.MA_DVIQLY == strMadviqly
                        orderby p.MA_DVIQLY ascending
                        select p;

                if (q != null && q.Count() > 0)
                {
                    DataTable dt = Utility.LINQToDataTable(q);
                    string strMaCT = dt.Rows[0]["MA_DVICTREN"].ToString();

                    if (strMaCT != string.Empty)
                    {
                        var w = from p in CMIS2.DB.D_DVI_QLY
                                where p.MA_DVIQLY == strMaCT
                                select p;
                        return w;
                    }
                    else
                    {
                        return null;
                    }
                }
                else return null;

               
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<D_DVI_QLY> select_D_DVI_QLY_All()
        {
            try
            {
                CMIS2.ResetDB();
                var q = from p in CMIS2.DB.D_DVI_QLY
                        orderby p.MA_DVIQLY ascending
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<D_DVI_QLY> select_D_DVI_QLY_Like(string strMaDviqly)
        {
            try
            {
                CMIS2.ResetDB();
                var q = from p in CMIS2.DB.D_DVI_QLY
                        orderby p.MA_DVICTREN, p.MA_DVIQLY ascending
                        where (p.MA_DVIQLY.Contains(strMaDviqly))
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<D_DVI_QLY> select_D_DVI_QLY_ID(string strMaDviqly)
        {
            try
            {
                CMIS2.ResetDB();
                var q = from p in CMIS2.DB.D_DVI_QLY
                        orderby p.MA_DVIQLY ascending
                        where (p.MA_DVIQLY == strMaDviqly)
                        select p;
                return q;
            }
            catch (Exception ex)
            {
                CMIS2.ResetDB();
                return null;
            }

        }
   

        public IEnumerable<D_DVI_QLY> select_D_DVI_QLY_IS_Captren(string strMaDviqly)
        {
            try
            {
                CMIS2.ResetDB();
                var q = from p in CMIS2.DB.D_DVI_QLY
                        orderby p.MA_DVIQLY ascending
                        where (p.MA_DVICTREN == strMaDviqly)
                        select p;
                return q;
            }
            catch (Exception ex)
            {
                CMIS2.ResetDB();
                return null;
            }

        }

        public IEnumerable<D_DVI_QLY> select_D_DVI_QLY_by_1_Col()
        {
            try
            {
                CMIS2.ResetDB();

                var q = from p in CMIS2.DB.D_DVI_QLY
                        orderby p.MA_DVIQLY, p.TEN_DVIQLY ascending
                        where (info.MA_DVIQLY != " " && p.MA_DVIQLY.ToUpper().Contains(info.MA_DVIQLY.ToUpper()) || info.MA_DVIQLY == " ")
                        && (info.TEN_DVIQLY != " " && p.TEN_DVIQLY.ToUpper().Contains(info.TEN_DVIQLY.ToUpper()) || info.TEN_DVIQLY == " ")
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public bool insert_D_DVI_QLY()
        {
            try
            {
                D_DVI_QLY objD_DVI_QLY = new D_DVI_QLY()
                {
                    MA_DVIQLY = info.MA_DVIQLY,
                    TEN_DVIQLY = info.TEN_DVIQLY,
                    MA_DVICTREN = info.MA_DVICTREN,
                    CAP_DVI = info.CAP_DVI,
                    DIA_CHI = info.DIA_CHI,
                    ID_DIA_CHINH = info.ID_DIA_CHINH,
                    DIEN_THOAI = info.DIEN_THOAI,
                    DTHOAI_KDOANH = info.DTHOAI_KDOANH,
                    DTHOAI_NONG = info.DTHOAI_NONG,
                    DTHOAI_TRUC = info.DTHOAI_TRUC,
                    FAX = info.FAX,
                    EMAIL = info.EMAIL,
                    MA_STHUE = info.MA_STHUE,
                    DAI_DIEN = info.DAI_DIEN,
                    CHUC_VU = info.CHUC_VU,
                    SO_UQUYEN = info.SO_UQUYEN,
                    NGAY_UQUYEN = info.NGAY_UQUYEN,
                    TEN_DVIUQ = info.TEN_DVIUQ,
                    DCHI_DVIUQ = info.DCHI_DVIUQ,
                    CVU_UQUYEN = info.CVU_UQUYEN,
                    TNGUOI_UQUYEN = info.TNGUOI_UQUYEN,
                    TEN_TINH = info.TEN_TINH,
                    WEBSITE = info.WEBSITE

                };
                CMIS2.DB.D_DVI_QLY.InsertOnSubmit(objD_DVI_QLY);
                CMIS2.DB.SubmitChanges();
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                CMIS2.ResetDB(); return false;
            }
        }

        /*
        public bool insert_VTT_TP(string id, string ten, string dienthoai)
        {
            //long _ID = GetMaxSequenceByBranch();
            //if (_ID == -1) return false;
            try
            {
                //info.TEN_NVIEN = Convert.ToInt16(_ID);
                VTT objVTT = new VTT()
                {
                    ID = info.ID,
                    TEN = info.TEN,
                    DIEN_THOAI = info.DIEN_THOAI
                };
                CMIS2.DB.VTT.InsertOnSubmit(info);
                CMIS2.DB.SubmitChanges();
                CMIS2.ResetDB();
                return true;
            }
            catch
            {
                CMIS2.ResetDB(); return false;
            }
        }
         * */

        public bool update_D_DVI_QLY()
        {
            try
            {
                D_DVI_QLY objD_DVI_QLY = CMIS2.DB.D_DVI_QLY.Single(c => c.MA_DVIQLY == info.MA_DVIQLY);

                objD_DVI_QLY.TEN_DVIQLY = info.TEN_DVIQLY;
                objD_DVI_QLY.MA_DVICTREN = info.MA_DVICTREN;
                objD_DVI_QLY.CAP_DVI = info.CAP_DVI;
                objD_DVI_QLY.DIA_CHI = info.DIA_CHI;
                objD_DVI_QLY.ID_DIA_CHINH = info.ID_DIA_CHINH;
                objD_DVI_QLY.DIEN_THOAI = info.DIEN_THOAI;
                objD_DVI_QLY.DTHOAI_KDOANH = info.DTHOAI_KDOANH;
                objD_DVI_QLY.DTHOAI_NONG = info.DTHOAI_NONG;
                objD_DVI_QLY.DTHOAI_TRUC = info.DTHOAI_TRUC;
                objD_DVI_QLY.FAX = info.FAX;
                objD_DVI_QLY.EMAIL = info.EMAIL;
                objD_DVI_QLY.MA_STHUE = info.MA_STHUE;
                objD_DVI_QLY.DAI_DIEN = info.DAI_DIEN;
                objD_DVI_QLY.CHUC_VU = info.CHUC_VU;
                objD_DVI_QLY.SO_UQUYEN = info.SO_UQUYEN;
                objD_DVI_QLY.NGAY_UQUYEN = info.NGAY_UQUYEN;
                objD_DVI_QLY.TEN_DVIUQ = info.TEN_DVIUQ;
                objD_DVI_QLY.DCHI_DVIUQ = info.DCHI_DVIUQ;
                objD_DVI_QLY.CVU_UQUYEN = info.CVU_UQUYEN;
                objD_DVI_QLY.TNGUOI_UQUYEN = info.TNGUOI_UQUYEN;
                objD_DVI_QLY.TEN_TINH = info.TEN_TINH;
                objD_DVI_QLY.WEBSITE = info.WEBSITE;

                CMIS2.DB.SubmitChanges();

                return true;
            }
            catch
            {
                CMIS2.ResetDB(); return false;
            }
        }

        public bool delete_D_DVI_QLY(ref string strErr)
        {
            try
            {
                var query = from p in CMIS2.DB.D_DVI_QLY where p.MA_DVIQLY == info.MA_DVIQLY select p;
                if (query != null && query.Count() > 0)
                {
                    CMIS2.DB.D_DVI_QLY.DeleteAllOnSubmit(query.ToList());
                    CMIS2.DB.SubmitChanges();
                    return true;
                }
                return true;
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                CMIS2.ResetDB(); return false;
            }
        }

        #endregion
    }
}
